package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.annotation.LoginUser;
import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.wx.WxAddressService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：Katherine
 * @Package：com.cskaoyan.market.controller.wx.teamwork
 * @Project：market
 * @name：AdminAdressController
 * @Date：2023/5/23 15:32
 * @Filename：AdminAdressController
 */
@RestController
@RequestMapping("/wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService adminAddressService;

    @GetMapping("list")
    public Object list() {
        List<MarketAddress> marketAddresses = adminAddressService.list();

        return ResponseUtil.okList(marketAddresses);
    }

    @GetMapping("detail")
    public Object detail(Integer id){

        MarketAddress marketAddresses = adminAddressService.detail(id);
        return ResponseUtil.ok(marketAddresses);
    }

    @PostMapping("save")
    @LoginUser
    public Object save(MarketUser marketUser, @RequestBody MarketAddress marketAddress){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        int save = adminAddressService.save(userId,marketAddress);

        if (save != 0){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();


    }

    @PostMapping("delete")
    public Object delete(@RequestBody MarketAddress marketAddress){
        Integer id = marketAddress.getId();
        int delete = adminAddressService.delete(id);

        if (delete==0){
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();

    }
}
